package com.iseastar.guojiang.newcabinet;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.AppLogic;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.shouhuobao.station.R;

/**
 * Created by joker on 2017/12/20.
 * 任务异常界面
 */
public class TaskExceptionActivity extends BaseActivity2 implements View.OnClickListener {

    private CheckBox mException1_CB;
    private CheckBox mException2_CB;
    private CheckBox mException3_CB;

    private ServiceTaskBean serviceTaskBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        serviceTaskBean = (ServiceTaskBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_task_exception);
        super.findViewById();
        getAppTitle().setCommonTitle("选择原因");
        mException1_CB = (CheckBox) findViewById(R.id.exception_checkbox1);
        mException1_CB.setOnClickListener(this);
        mException2_CB = (CheckBox) findViewById(R.id.exception_checkbox2);
        mException2_CB.setOnClickListener(this);
        mException3_CB = (CheckBox) findViewById(R.id.exception_checkbox3);
        mException3_CB.setOnClickListener(this);
        findViewById(R.id.submt_tv).setOnClickListener(this);
        findViewById(R.id.service_phone_tv).setOnClickListener(this);
        if (null != serviceTaskBean) {
            //上门和驿站显示一样
            if (Const.CourierTaskStatus.visitTask == serviceTaskBean.getTaskType() || Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType() || Const.CourierTaskStatus.sameCityTask == serviceTaskBean.getTaskType()) {
                if (Const.CourierTaskStatus.visitTask == serviceTaskBean.getTaskType()) {
                    ((TextView) findViewById(R.id.exception_title_tv)).setText("寄件人原因无法取件(无法取得联系，地址无人等)");
                    findViewById(R.id.exception_item3).setVisibility(View.GONE);
                } else if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()) {
                    findViewById(R.id.exception_item3).setVisibility(View.GONE);
                } else {
                    ((TextView) findViewById(R.id.exception_title_tv)).setText("寄件人原因无法取件(无法取得联系，地址无人等)");
                    findViewById(R.id.exception_item3).setVisibility(View.VISIBLE);
                    findViewById(R.id.exception_item3).setOnClickListener(this);
                }
                findViewById(R.id.exception_item1).setOnClickListener(this);
                findViewById(R.id.exception_item2).setOnClickListener(this);
            } else {//快递柜显示
                findViewById(R.id.exception_item1).setVisibility(View.GONE);
                findViewById(R.id.exception_item2).setOnClickListener(this);
                findViewById(R.id.exception_item3).setVisibility(View.GONE);
            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //异常1
            case R.id.exception_item1:
            case R.id.exception_checkbox1:
                mException1_CB.setChecked(true);
                mException2_CB.setChecked(false);
                mException3_CB.setChecked(false);
                break;
            //异常2
            case R.id.exception_item2:
            case R.id.exception_checkbox2:
                mException1_CB.setChecked(false);
                mException2_CB.setChecked(true);
                mException3_CB.setChecked(false);
                break;
            //异常3
            case R.id.exception_item3:
            case R.id.exception_checkbox3:
                mException1_CB.setChecked(false);
                mException2_CB.setChecked(false);
                mException3_CB.setChecked(true);
                break;
            //提交异常
            case R.id.submt_tv:
                if (serviceTaskBean == null) {
                    return;
                }
                if (!mException1_CB.isChecked() && !mException2_CB.isChecked() && !mException1_CB.isChecked()) {
                    showToast("请选择异常原因");
                    return;
                }
                showLoadingDialog(null);
                if (mException1_CB.isChecked()) {
                    AppHttp.getInstance().courierTaskException(serviceTaskBean.getTaskId(), 5, "");
                } else if (mException2_CB.isChecked()) {
                    AppHttp.getInstance().courierTaskException(serviceTaskBean.getTaskId(), 6, "");
                } else if (mException3_CB.isChecked()) {
                    AppHttp.getInstance().courierTaskException(serviceTaskBean.getTaskId(), 7, "");
                }
                break;
            //拨打客服电话
            case R.id.service_phone_tv:
                Intents.dialOrCall("400-617-1949");
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.courier_task_exception:
                try {
                    ReqResult<?> result = JSON.parser(msg.obj);
                    if (checkLoginStatus(result)) {
                        if ((Boolean) result.getResult()) {
                            showToast("任务取消成功");
                            AppLogic.courierBackToHomeAndRefresh(getContext());
                            finish();
                        } else {
                            showToast("任务异常");
                        }
                    } else {
                        showToast(result.getMessage());
                    }
                } catch (Exception e) {
                    showToast("任务异常");
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
